<template>
  <div class="container">
    <el-dialog title="订单修改" :visible.sync="detailsDialog.centerDialogVisible" @open="getPackageBox()" @close="cancel"
      center width="99%">
      <div class="top">
        <el-switch v-model="is_assign" active-text="批次出(成本价)" inactive-text="先入先出(平均价)" @change="assign(is_assign)">
        </el-switch>
        <el-button size="medium" type="primary" class="handleSearch" icon="el-icon-plus" v-if="is_assign"
          @click="addReturn()">获取到货价</el-button>
      </div>
      <!-- <div class="top" v-if="type != 2">
        <el-select
          :multiple="false"
          filterable
          remote
          center
          reserve-keyword
          v-model="commodity"
          placeholder="请搜索选择商品"
          :remote-method="insideRemoteMethod"
          :loading="inside.loading"
        >
          <el-option
            v-for="item in inside.option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          size="medium"
          type="primary"
          class="handleSearch"
          icon="el-icon-search"
          @click="addGoods()"
        >添加</el-button>
      </div> -->
      <el-table :data="tableData" border max-height="500" style="width: 100%" empty-text="暂无数据">
        <el-table-column prop="goods_name" label="商品名" align="center"></el-table-column>
        <el-table-column prop="erp_goods_code" label="商品编码" align="center"></el-table-column>
        <el-table-column prop="goods_spec" label="规格" align="center"></el-table-column>
        <el-table-column prop="category_code" label="分类编码" align="center"></el-table-column>
        <el-table-column prop="category_name" label="分类名称" align="center"></el-table-column>
        <el-table-column prop="goods_unit" label="单位" align="center"></el-table-column>
        <el-table-column prop="bar_code" label="条形码" align="center"></el-table-column>
        <el-table-column prop="purchasePrice" v-if="!is_assign" label="采购加价" align="center"></el-table-column>
        <el-table-column v-else prop="purchasePrices" label="采购加价" align="center"></el-table-column>
        <el-table-column v-if="!is_assign" prop="avg_price" label="平均价" align="center"></el-table-column>
        <el-table-column v-else prop="cost_price" label="成本价" align="center"></el-table-column>

        <el-table-column label="参考价" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.status != 1">{{ scope.row.cost_price }}</div>
            <el-input-number v-else size="small" :controls="false" v-model="scope.row.cost_price"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column v-if="!is_assign" prop="stockRemoval" label="出库价" align="center"></el-table-column>
        <el-table-column v-else prop="stockRemovals" label="出库价" align="center"></el-table-column>
        <el-table-column v-if="!is_assign" prop="totalPrices" label="出库价总价" align="center"></el-table-column>
        <el-table-column v-else prop="totalPricess" label="出库价总价" align="center"></el-table-column>
        <el-table-column v-if="!is_assign" prop="rk_spec" label="箱规" align="center"></el-table-column>
        <el-table-column v-else prop="rk_specs" label="箱规" align="center"></el-table-column>
        <el-table-column v-if="is_assign" label="生产日期" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.produce_date }}</p>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="到期日" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.due_date }}</p>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="保质期（天）" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.shelf_life }}</p>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="批次商品" align="center" width="150px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.batch_id" placeholder="请选择批次商品" @change="onBatch(scope)">
              <el-option v-for="item in scope.row.unAvgBatchList" :key="item.value" :label="item.label"
                :value="item.value" :title="item.code">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column v-if="is_assign" label="门店到货价" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number size="small" @change="orderFixedPprice(scope)" :controls="false"
              v-model="scope.row.fixed_price"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="商品类型" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.status != 1">{{ scope.row.isXcMsg }}</div>
            <el-select v-else v-model="scope.row.is_zc" placeholder="请选择商品类型">
              <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <!-- <el-table-column label="供应商" align="center" width="150px">
          <template slot-scope="scope">
            <div v-if="scope.row.status != 1">
              {{ scope.row.supplier_name }}
            </div>
            <el-select
              v-else
              size="small"
              v-model="scope.row.supplierName"
              :multiple="false"
              filterable
              remote
              center
              reserve-keyword
              placeholder="请选择供应商"
              :remote-method="supplierMethod"
              :loading="insides.loading"
              @change="getOptionId(scope.row.supplierName, scope)"
            >
              <el-option
                v-for="item in insides.option"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
        </el-table-column> -->
        <el-table-column v-if="!is_assign" prop="stock_num" label="库存" align="center"></el-table-column>
        <el-table-column v-else prop="batch_stock_num" label="库存" align="center"></el-table-column>
        <el-table-column label="数量" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number size="small" @change="orderNum(scope)" :controls="false"
              v-model="scope.row.order_num"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="加价" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number size="small" @change="markup(scope)" :controls="false" v-model="scope.row.markup"
              v-if="!is_assign"></el-input-number>
            <el-input-number v-else size="small" :disabled="scope.row.fixed_priceType" @change="markup(scope)"
              :controls="false" v-model="scope.row.markups"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="件数" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number @change="qtyup(scope)" size="small" :controls="false"
              v-model="scope.row.box_qty"></el-input-number>
          </template>
        </el-table-column>
        <!-- <el-table-column label="预提费" align="center" width="150px">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              @change="markup(scope)"
              :controls="false"
              v-model="scope.row.withholding_fee"
            ></el-input-number>
          </template>
        </el-table-column> -->
        <el-table-column v-if="!is_assign" label="预提" align="center" width="150px">
          <template slot-scope="scope">
            <div>{{ scope.row.withholding_fee }}</div>
          </template>
        </el-table-column>
        <el-table-column v-else label="预提" align="center" width="150px">
          <template slot-scope="scope">
            <div>{{ scope.row.withholding_fee2 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="包装箱" align="center" width="150px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.package_box_id" @change="rdcBox(scope)" placeholder="请选择">
              <el-option v-for="item in boxList" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="包装箱规格" align="center" width="200px">
          <template slot-scope="scope">
            <div>
              <el-input-number size="small" :precision="4" v-if="scope.row.rdcType" :controls="false"
                v-model="scope.row.package_box_specInp"></el-input-number>
              <span v-else>{{ scope.row.package_box_specInp }}</span>
              <span v-if="scope.row.rdcType" style="line-height: 30px;margin-left: 5px;">KG</span>
              <span v-else style="line-height: 30px;margin-left: 5px;">个</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" width="250px">
          <template slot-scope="scope">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="scope.row.notes">
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button size="small" v-if="scope.row.status == 1 && type != 2" type="danger" @click="remove(scope)"
              icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  findOrderInfo,
  editOrderInfo,
  getFixedPrice,
  getPackageBoxData
} from "@/api/superOrder/superOrderList";
import { selectErpOrgGoodsData } from "@/api/commodity/commodityList"; //商品
import { selectSupplierList } from "@/api/putStorage/superPurchaseList";

export default {
  props: {
    detailsDialog: Object,
  },
  data() {
    return {
      disabled: true,
      tableData: [],
      commodity: "",
      is_assign: false,
      inside: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      },
      goodsList: [],
      org_area_code: "",
      insides: {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      },
      typeList: [
        {
          value: -1,
          label: "供应商商品",
        },
        {
          value: 1,
          label: "中仓商品",
        },
      ],
      type: null,
      boxData: {},
      boxList: [],
      rdc: null,
    };
  },
  methods: {
    cancel() {
      this.detailsDialog.centerDialogVisible = false;
      this.inside = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      };
      this.insides = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      };
      this.commodity = "";
    },
    getPackageBox() {
      getPackageBoxData().then(res => {
        this.boxData = res.data.package;
        this.rdc = res.data.rdc
        this.getData(true);
      })
    },
    rdcBox(scope) {
      let id = scope.row.package_box_id;
      this.boxList.forEach((res, index) => {
        if (res.id == scope.row.package_box_id) {
          if (index == 0) {
            scope.row.rdcType = false;
            scope.row.package_box_specInp = 1
          } else {
            scope.row.rdcType = true
            scope.row.package_box_specInp = scope.row.package_box_spec
          }
        }
      })
    },
    getData(is_assign = "", flag = false) {
      let data = {
        id: this.detailsDialog.id,
      };
      findOrderInfo(data).then((res) => {
        if (res.code == 0) {
          this.org_area_code = res.data.info.rdc_area_code;
          if (res.data.info.rdc_attr == '0') {
            this.boxList = this.boxData.shengnei;
          } else {
            this.boxList = this.boxData.shengwai

          }
          let rdcBool = null;
          res.data.info.order_goods.forEach((result) => {
            this.boxList.forEach((prop, index) => {
              if (prop.id == result.package_box_id) {
                if (index == 0) {
                  rdcBool = false
                } else {
                  rdcBool = true
                }
              }
            })

            result.rdcType = rdcBool;
            result.package_box_specInp = result.package_box_spec;
            result.unAvgBatchList = [];
            result.isXcMsg = result.is_zc == 1 ? "中仓商品" : "供应商商品";
            result.batch_id = result.batch_id == "0" ? "" : result.batch_id;
            result.markup = result.markup;
            result.markups = result.markup2;
            result.fixed_priceType = parseFloat(result.fixed_price)
              ? true
              : false;

            result.purchasePrice = parseFloat(result.buyerprofit_price).toFixed(
              4
            );
            result.purchasePrices = parseFloat(
              result.buyerprofit_price2
            ).toFixed(4);
            result.stockRemoval = (
              parseFloat(result.avg_price) +
              parseFloat(result.markup) +
              parseFloat(result.withholding_fee) +
              parseFloat(result.purchasePrice)
            ).toFixed(4);
            result.totalPrices = (
              result.stockRemoval * result.order_num
            ).toFixed(4);

            result.stockRemovals = (
              parseFloat(result.cost_price) +
              parseFloat(result.markups) +
              parseFloat(result.withholding_fee2) +
              parseFloat(result.purchasePrices)
            ).toFixed(4);
            result.totalPricess = (
              result.stockRemovals * result.order_num
            ).toFixed(4);
            result.rk_specs = result.rk_spec;

            result.rk_spec =
              flag === true ? result.goods_rk_spec : result.rk_spec;
            if (!is_assign) {
              let rk_spec = parseFloat(result.rk_spec);
              if (rk_spec) {
                result.box_qty = (result.order_num / rk_spec).toFixed(4);
              }
            }
            result.batch_list.forEach((item) => {
              if (item.id == result.batch_id) {
                result.produce_date = item.produce_date
                result.due_date = item.due_date
                result.shelf_life = item.shelf_life
              }
              let codeList = item.code.split(",");
              let codeStr =
                codeList.length > 1 ? codeList[0] + "..." : codeList[0];
              result.unAvgBatchList.push({
                value: `${item.id}`,
                label: `${item.goods_name}【${codeStr}】【${item.supplier_name}:${item.supplier_code}】
【生产日期:${item.produce_date ? item.produce_date : '无'}】(${item.stock_num}/${item.rk_spec})`,
                price: `${item.price}`,
                stock_num: `${item.stock_num}`,
                withholding_fee: `${item.withholding_fee}`,
                rk_spec: `${item.rk_spec}`,
                code: `${item.code}`,
                produce_date: `${item.produce_date}`,
                shelf_life: `${item.shelf_life}`,
                due_date: `${item.due_date}`,
              });
            });
          });
          if (is_assign) {
            this.is_assign = res.data.info.is_assign == 1 ? true : false;
          }
          this.tableData = res.data.info.order_goods;
          this.type = res.data.info.type;
        }
      });
      let datas = {
        page: 1,
        limit: 100,
      };
      //获取供应商列表
      selectSupplierList(datas).then((res) => {
        if (res.data.paginate.data || res.data.paginate.data.length > 0) {
          this.insides.option = res.data.paginate.data.map((item) => {
            return {
              value: `${item.id}`,
              label: `${item.supplier_name}`,
              code: `${item.supplier_code}`,
            };
          });
        }
      });

      // let datass = {
      //   org_code: this.detailsDialog.erp_org_code,
      // };
      // //获取商品
      // selectErpOrgGoodsData(datass).then((res) => {
      //   this.goodsList = res.data.paginate.data;
      //   if (res.data.paginate.data) {
      //     this.inside.option = res.data.paginate.data.map((item) => {
      //       return { value: `${item.Code}`, label: `${item.Name}` };
      //     });
      //   }
      // });
    },

    //删除
    remove(scope) {
      let id = scope.row.Code;
      this.tableData.forEach((res, index) => {
        if (res.Code == id) {
          this.tableData.splice(index, 1);
        }
      });
    },
    // 搜索商品
    addGoods() {
      if (this.commodity) {
        let every = this.tableData.filter((res) => {
          return (
            res.Code == this.commodity || res.erp_goods_code == this.commodity
          );
        });
        if (every.length == 0) {
          let arr = this.goodsList.filter((res) => {
            return res.Code == this.commodity;
          });
          this.tableData.push({
            erp_goods_code: arr[0].Code,
            goods_name: arr[0].Name,
            bar_code: arr[0].BarCode,
            goods_unit: arr[0].Unit,
            goods_spec: arr[0].Spec || "",
            category_code: arr[0].ClassCode,
            category_name: arr[0].ClassName,
            Code: arr[0].Code,
            order_num: 0,
            box_qty: 0,
            supplier_id: 0,
            supplier_code: 0,
            is_zc: "",
            cost_price: 0,
            withholding_fee: 0,
            markup: 0,
            status: 1,
            supplierName: "",
          });
        } else {
          this.$message({
            type: "info",
            message: "该商品已添加",
          });
        }
      } else {
        this.$message({
          type: "info",
          message: "请选择商品",
        });
      }
    },
    //修改加价率和预提
    markup(scope) {
      //先入先出计算
      let purchasePriceCenter = (
        Math.round(
          (parseFloat(scope.row.avg_price) +
            parseFloat(scope.row.withholding_fee) +
            parseFloat(scope.row.markup)) *
          parseFloat(scope.row.buyerprofitrate) *
          10000
        ) / 10000
      ).toFixed(4);
      let purchasePriceCenters = (
        Math.round(
          (parseFloat(scope.row.cost_price) +
            parseFloat(scope.row.withholding_fee2) +
            parseFloat(scope.row.markups)) *
          parseFloat(scope.row.buyerprofitrate) *
          10000
        ) / 10000
      ).toFixed(4);
      scope.row.purchasePrice = parseInt(scope.row.buyerprofitmode)
        ? scope.row.buyerprofitrate
        : purchasePriceCenter;
      scope.row.purchasePrices = parseInt(scope.row.buyerprofitmode)
        ? scope.row.buyerprofitrate
        : purchasePriceCenters;
      scope.row.stockRemoval = (
        parseFloat(scope.row.avg_price) +
        parseFloat(scope.row.markup) +
        parseFloat(scope.row.withholding_fee) +
        parseFloat(scope.row.purchasePrice)
      ).toFixed(4);
      scope.row.totalPrices = (
        scope.row.stockRemoval * scope.row.order_num
      ).toFixed(4);
      this.$set(this.tableData, scope.$index, scope.row);
      //批次计算
      scope.row.stockRemovals = (
        parseFloat(scope.row.cost_price) +
        parseFloat(scope.row.markups) +
        parseFloat(scope.row.withholding_fee2) +
        parseFloat(scope.row.purchasePrices)
      ).toFixed(4);
      scope.row.totalPricess = (
        scope.row.stockRemovals * scope.row.order_num
      ).toFixed(4);
      this.$set(this.tableData, scope.$index, scope.row);
    },
    //修改数量
    orderNum(scope) {
      if (this.is_assign) {
        if (scope.row.rk_specs) {
          if (scope.row.order_num) {
            let rk_specs = parseFloat(scope.row.rk_specs);
            scope.row.box_qty = (scope.row.order_num / rk_specs).toFixed(4);
          } else {
            scope.row.order_num = 0;
            let rk_specs = parseFloat(scope.row.rk_specs);
            scope.row.box_qty = (scope.row.order_num / rk_specs).toFixed(4);
          }
        }
      } else {
        if (scope.row.rk_spec) {
          if (scope.row.order_num) {
            scope.row.box_qty = (
              scope.row.order_num / scope.row.rk_spec
            ).toFixed(4);
          } else {
            scope.row.order_num = 0;
            scope.row.box_qty = (
              scope.row.order_num / scope.row.rk_spec
            ).toFixed(4);
          }
        }
      }
      scope.row.totalPrices = (
        scope.row.stockRemoval * scope.row.order_num
      ).toFixed(4);

      scope.row.totalPricess = (
        scope.row.stockRemovals * scope.row.order_num
      ).toFixed(4);

      this.$set(this.tableData, scope.$index, scope.row);
    },
    //修改件数
    qtyup(scope) {
      if (scope.row.rk_spec) {
        if (scope.row.box_qty) {
          scope.row.order_num = (scope.row.box_qty * scope.row.rk_spec).toFixed(
            4
          );
        } else {
          scope.row.box_qty = 0;
          scope.row.order_num = (scope.row.box_qty * scope.row.rk_spec).toFixed(
            4
          );
        }
      }
      if (this.is_assign) {
        if (scope.row.rk_specs) {
          let rk_specs = parseFloat(scope.row.rk_specs);
          if (scope.row.box_qty) {
            scope.row.order_num = (
              scope.row.box_qty * scope.row.rk_specs
            ).toFixed(4);
          } else {
            scope.row.box_qty = 0;
            scope.row.order_num = (
              scope.row.box_qty * scope.row.rk_specs
            ).toFixed(4);
          }
        }
      }
    },
    //选择批次
    onBatch(scope) {
      let center = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[0];
      scope.row.rk_specs = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[1];
      scope.row.produce_date = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[2]
      scope.row.shelf_life = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[3]
      scope.row.due_date = this.batchPrice(
        scope.row.batch_id,
        scope.row.unAvgBatchList
      )[4]
      if (scope.row.rk_specs) {
        let rk_specs = parseFloat(scope.row.rk_specs);
        scope.row.box_qty = (scope.row.order_num / rk_specs).toFixed(4);
      }
      scope.row.cost_price = center.price;
      scope.row.batch_stock_num = center.stock_num;
      scope.row.withholding_fee2 = center.withholding_fee;
      if (scope.row.fixed_priceType) {
        scope.row.stockRemovals = scope.row.fixed_price;
        if (!parseInt(scope.row.buyerprofitmode)) {
          let num = (
            parseFloat(scope.row.fixed_price) /
            (1 + parseFloat(scope.row.buyerprofitrate))
          ).toFixed(4);
          scope.row.purchasePrices = (
            parseFloat(scope.row.fixed_price) - num
          ).toFixed(4);
        } else {
          scope.row.purchasePrices = parseFloat(
            scope.row.buyerprofitrate
          ).toFixed(4);
        }
        scope.row.markups = (
          parseFloat(scope.row.fixed_price) -
          parseFloat(scope.row.cost_price) -
          parseFloat(scope.row.withholding_fee2) -
          parseFloat(scope.row.purchasePrices)
        ).toFixed(4);
        // scope.row.purchasePrices = 0;
        // scope.row.markups = (
        //   parseFloat(scope.row.fixed_price) -
        //   parseFloat(scope.row.withholding_fee2) -
        //   parseFloat(scope.row.cost_price)
        // ).toFixed(4);
      } else {
        let purchasePriceCenters = (
          (parseFloat(scope.row.cost_price) +
            parseFloat(scope.row.markups) +
            parseFloat(scope.row.withholding_fee2)) *
          parseFloat(scope.row.buyerprofitrate)
        ).toFixed(4);
        scope.row.purchasePrices = parseInt(scope.row.buyerprofitmode)
          ? scope.row.buyerprofitrate
          : purchasePriceCenters;

        scope.row.stockRemovals = (
          parseFloat(scope.row.cost_price) +
          parseFloat(scope.row.markups) +
          parseFloat(scope.row.withholding_fee2) +
          parseFloat(scope.row.purchasePrices)
        ).toFixed(4);
      }
      scope.row.totalPricess = (
        scope.row.stockRemovals * scope.row.order_num
      ).toFixed(4);
    },
    batchPrice(id, unAvgBatchList) {
      let rk_specs = 0;
      let center = {
        price: 0,
        stock_num: 0,
      };
      let produce_date = ''
      let shelf_life = ''
      let due_date = ''
      unAvgBatchList.forEach((res) => {
        if (res.value == id) {
          center.price = res.price;
          center.stock_num = res.stock_num;
          center.withholding_fee = res.withholding_fee;
          rk_specs = res.rk_spec;
          produce_date = res.produce_date
          shelf_life = res.shelf_life
          due_date = res.due_date
        }
      });
      return [center, rk_specs, produce_date, shelf_life, due_date];
    },
    assign(is_assign) {
      if (!is_assign) {
        this.getData(false, true);
      } else {
        this.tableData.forEach((res) => {
          let rk_specs = parseFloat(res.rk_specs);
          if (rk_specs) {
            res.box_qty = (res.order_num / rk_specs).toFixed(4);
          } else {
            scope.box_qty = 0;
          }
          if (res.batch_id == 0) {
            res.stock_num = 0;
          }
        });
      }
    },
    //修改门店到货价
    orderFixedPprice(scope) {
      if (parseFloat(scope.row.fixed_price)) {
        scope.row.fixed_priceType = true;
        scope.row.stockRemovals = scope.row.fixed_price;
        // scope.row.purchasePrices = 0;
        // scope.row.markups = (
        //   parseFloat(scope.row.fixed_price) -
        //   parseFloat(scope.row.withholding_fee2) -
        //   parseFloat(scope.row.cost_price)
        // ).toFixed(4);
        if (!parseInt(scope.row.buyerprofitmode)) {
          let num = (
            parseFloat(scope.row.fixed_price) /
            (1 + parseFloat(scope.row.buyerprofitrate))
          ).toFixed(4);
          scope.row.purchasePrices = (
            parseFloat(scope.row.fixed_price) - num
          ).toFixed(4);
        } else {
          scope.row.purchasePrices = parseFloat(
            scope.row.buyerprofitrate
          ).toFixed(4);
        }
        scope.row.markups = (
          parseFloat(scope.row.fixed_price) -
          parseFloat(scope.row.cost_price) -
          parseFloat(scope.row.withholding_fee2) -
          parseFloat(scope.row.purchasePrices)
        ).toFixed(4);
        scope.row.totalPrices = (
          scope.row.stockRemoval * scope.row.order_num
        ).toFixed(4);

        scope.row.totalPricess = (
          scope.row.stockRemovals * scope.row.order_num
        ).toFixed(4);
      } else {
        scope.row.fixed_priceType = false;
        // scope.row.fixed_price = 0;
        // let purchasePriceCenters = (
        //   (parseFloat(scope.row.cost_price) +
        //     parseFloat(scope.row.markup) +
        //     parseFloat(scope.row.withholding_fee2)) *
        //   parseFloat(scope.row.buyerprofitrate)
        // ).toFixed(4);
        // scope.row.purchasePrices = scope.row.buyerprofitmode
        //   ? scope.row.buyerprofitrate
        //   : purchasePriceCenters;

        // scope.row.stockRemovals = (
        //   parseFloat(scope.row.cost_price) +
        //   parseFloat(scope.row.markup) +
        //   parseFloat(scope.row.withholding_fee2) +
        //   parseFloat(scope.row.purchasePrices)
        // ).toFixed(4);
        // scope.row.totalPrices = (
        //   scope.row.stockRemoval * scope.row.order_num
        // ).toFixed(4);

        // scope.row.totalPricess = (
        //   scope.row.stockRemovals * scope.row.order_num
        // ).toFixed(4);
      }
    },
    // 获取商品方法
    // insideRemoteMethod(query) {
    //   if (query !== "") {
    //     this.getSupplierList(query);
    //   } else {
    //     this.inside.options = [];
    //   }
    // },
    //获取商品数据
    // getSupplierList(query) {
    //   let data = {
    //     goods_code: query,
    //     org_code: this.detailsDialog.erp_org_code,
    //   };
    //   this.inside.loading = true;
    //   selectErpOrgGoodsData(data).then((res) => {
    //     this.goodsList = res.data.paginate.data;
    //     if (res.data.paginate.data) {
    //       this.inside.list = res.data.paginate.data.map((item) => {
    //         return { value: `${item.Code}`, label: `${item.Name}` };
    //       });
    //       this.inside.loading = false;
    //       this.inside.option = this.inside.list.filter((item) => {
    //         return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
    //       });
    //     } else {
    //       this.inside.loading = false;
    //       this.inside.option = [];
    //     }
    //   });
    // },
    // 嵌套搜索供应商数据
    supplierMethod(query) {
      if (query !== "") {
        this.supplierList(query);
      } else {
        this.insides.option = [];
      }
    },
    //获取供应商数据
    supplierList(query) {
      let data = {
        name: query,
        page: 1,
        limit: 100,
      };
      this.insides.loading = true;
      selectSupplierList(data).then((res) => {
        this.insides.list = res.data.paginate.data.map((item) => {
          return {
            value: `${item.id}`,
            label: `${item.supplier_name}`,
            code: `${item.supplier_code}`,
          };
        });
        this.insides.loading = false;
        this.insides.option = this.insides.list.filter((item) => {
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      });
    },
    //选择供应商获取ID
    getOptionId(e, scope) {
      let data = this.insides.option.filter((res) => {
        return res.value == e;
      });


      this.tableData[scope.$index].supplier_id = data[0].value;
      this.tableData[scope.$index].supplier_code = data[0].code;
      this.initSuoolier();
    },
    //初始化供应商搜索
    initSuoolier() {
      this.insides = {
        innerVisible: false,
        option: [],
        value: "",
        list: [],
        loading: false,
      };
      let datas = {
        page: 1,
        limit: 100,
      };
      //获取供应商列表
      selectSupplierList(datas).then((res) => {
        this.insides.option = res.data.paginate.data.map((item) => {
          return {
            value: `${item.id}`,
            label: `${item.supplier_name}`,
            code: `${item.supplier_code}`,
          };
        });
      });
    },
    //确定
    confirm() {
      let arr = [];
      this.tableData.forEach((res) => {
        if (this.is_assign) {
          arr.push({
            erp_goods_code: res.erp_goods_code,
            goods_name: res.goods_name,
            bar_code: res.bar_code,
            goods_unit: res.goods_unit,
            goods_spec: res.goods_spec || "",
            category_code: res.category_code,
            category_name: res.category_name,
            is_zc: 1,
            cost_price: res.cost_price,
            order_num: res.order_num,
            box_qty: res.box_qty,
            withholding_fee: res.withholding_fee2,
            markup: res.markups,
            batch_id: res.batch_id,
            id: res.id,
            fixed_price: res.fixed_price,
            buyerprofitmode: res.buyerprofitmode,
            buyerprofitrate: res.buyerprofitrate,
            rk_spec: res.rk_specs,
            package_box_id: res.package_box_id,
            package_box_spec: res.package_box_specInp,
            // produce_date: res.produce_date,
            // shelf_life: res.shelf_life,
            // due_date: res.due_date,
            notes: res.notes
          });
        } else {
          arr.push({
            erp_goods_code: res.erp_goods_code,
            goods_name: res.goods_name,
            bar_code: res.bar_code,
            goods_unit: res.goods_unit,
            goods_spec: res.goods_spec || "",
            category_code: res.category_code,
            category_name: res.category_name,
            is_zc: 1,
            order_num: res.order_num,
            box_qty: res.box_qty,
            withholding_fee: res.withholding_fee,
            markup: res.markup,
            id: res.id,
            buyerprofitmode: res.buyerprofitmode,
            buyerprofitrate: res.buyerprofitrate,
            rk_spec: res.rk_spec,
            package_box_id: res.package_box_id,
            package_box_spec: res.package_box_specInp,
            notes: res.notes
          });
        }
      });
      let data = {
        is_assign: this.is_assign ? 1 : 0,
        goods: JSON.stringify(arr),
        id: this.detailsDialog.id,
      };
      editOrderInfo(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
          this.$emit("upData");
          this.cancel();
        }
      });
    },
    //获取到货价
    addReturn() {
      let codeList = [];
      this.tableData.forEach((prop) => {
        codeList.push(prop.erp_goods_code);
      });
      let data = {
        erp_goods_codes: codeList.join(","),
        org_area_code: this.org_area_code
      };
      getFixedPrice(data).then((res) => {
        if (JSON.stringify(res.data.fixed_price_list)) {
          for (var i in res.data.fixed_price_list) {
            this.tableData.forEach((prop) => {
              if (i == prop.erp_goods_code) {
                prop.fixed_price = res.data.fixed_price_list[i];
                let scope = {};
                scope.row = prop;
                this.orderFixedPprices(scope);
              }
            });
          }
        }
      });
    },
    //获取取门店到货价并修改
    orderFixedPprices(scope) {
      if (parseFloat(scope.row.fixed_price)) {
        scope.row.fixed_priceType = true;
        scope.row.stockRemovals = scope.row.fixed_price;
        // scope.row.purchasePrices = 0;
        // scope.row.markups = (
        //   parseFloat(scope.row.fixed_price) -
        //   parseFloat(scope.row.withholding_fee2) -
        //   parseFloat(scope.row.cost_price)
        // ).toFixed(4);
        if (!parseInt(scope.row.buyerprofitmode)) {
          let num = (
            parseFloat(scope.row.fixed_price) /
            (1 + parseFloat(scope.row.buyerprofitrate))
          ).toFixed(4);
          scope.row.purchasePrices = (
            parseFloat(scope.row.fixed_price) - num
          ).toFixed(4);
        } else {
          scope.row.purchasePrices = parseFloat(
            scope.row.buyerprofitrate
          ).toFixed(4);
        }
        scope.row.markups = (
          parseFloat(scope.row.fixed_price) -
          parseFloat(scope.row.cost_price) -
          parseFloat(scope.row.withholding_fee2) -
          parseFloat(scope.row.purchasePrices)
        ).toFixed(4);
        scope.row.totalPrices = (
          scope.row.stockRemoval * scope.row.order_num
        ).toFixed(4);

        scope.row.totalPricess = (
          scope.row.stockRemovals * scope.row.order_num
        ).toFixed(4);
      } else {
        scope.row.fixed_priceType = false;
        // scope.row.fixed_price = 0;
        // let purchasePriceCenters = (
        //   (parseFloat(scope.row.cost_price) +
        //     parseFloat(scope.row.markup) +
        //     parseFloat(scope.row.withholding_fee2)) *
        //   parseFloat(scope.row.buyerprofitrate)
        // ).toFixed(4);
        // scope.row.purchasePrices = scope.row.buyerprofitmode
        //   ? scope.row.buyerprofitrate
        //   : purchasePriceCenters;

        // scope.row.stockRemovals = (
        //   parseFloat(scope.row.cost_price) +
        //   parseFloat(scope.row.markup) +
        //   parseFloat(scope.row.withholding_fee2) +
        //   parseFloat(scope.row.purchasePrices)
        // ).toFixed(4);
        // scope.row.totalPrices = (
        //   scope.row.stockRemoval * scope.row.order_num
        // ).toFixed(4);

        // scope.row.totalPricess = (
        //   scope.row.stockRemovals * scope.row.order_num
        // ).toFixed(4);
      }
    },
  },
};
</script>
<style scoped>
.pagination {
  text-align: right;
  padding: 10px;
}

.top {
  margin-bottom: 5px;
}
</style>